﻿using AlibabaCloud.SDK.Dysmsapi20170525.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using OSharp.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
 


namespace SMSSendTest
{
    public static class AliYunSmsService
    {
        private static readonly string apiUrl = "http://api.ums86.com:8899/sms/Api/Send.do";

       

        public static AlibabaCloud.SDK.Dysmsapi20170525.Client CreateClient()
        {
            // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
            // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378671.html。
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                AccessKeyId = "LTAI5t8NMi3Q4arbtNFcGQAZ",
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                AccessKeySecret = "QswD1GtZgURpfpLL7svAxRfn5QKASa",
            };
            // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
            config.Endpoint = "dysmsapi.aliyuncs.com";
            return new AlibabaCloud.SDK.Dysmsapi20170525.Client(config);
        }

        public static SendBatchSmsResponseBody SendSmsAsync(List<string> phones,List<string> signs,List<string> templateParams,string templateCode)
        {
            AlibabaCloud.SDK.Dysmsapi20170525.Client client = CreateClient();
            var  request = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendBatchSmsRequest();
          
            try
            {
                request.PhoneNumberJson = phones.ToJson();
                request.SignNameJson = signs.ToJson();
                request.TemplateCode = templateCode;
                request.TemplateParamJson = templateParams.ToJson();
                // 复制代码运行请自行打印 API 的返回值

                var response = client.SendBatchSms(request);
                //"162415331050747967^0"
                //162415331050747967^0
                //753916831051241861^0
                //"325712031053869790^0"   "2CAF3941-E004-50E6-8029-FECCD72CAD36"
                return response.Body;
                
            }
            catch (Exception error)
            {
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            }
            return null;
             
        }

        private static string ToJson(this object obj)
        {
            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            return JsonConvert.SerializeObject(obj, timeConverter);
        }

        public  static QuerySendDetailsResponseBody   QuerySendDetailsRequest(string bizId)
        {
            AlibabaCloud.SDK.Dysmsapi20170525.Client client = CreateClient();


            QuerySendDetailsRequest request = new QuerySendDetailsRequest
            {
                PhoneNumber = "13523591270", // 要查询的手机号码
                BizId = string.IsNullOrEmpty(bizId)?"206123231054334669^0": bizId,             // 短信发送接口返回的BizId
                SendDate = DateTime.Now.ToString("yyyyMMdd"),
                PageSize = 10,                  // 分页大小
                CurrentPage = 1                 // 当前页码
            };
            
            try
            {
                var response = client.QuerySendDetails(request);

                return response.Body;
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
            }
            return null;
        }
    }
}
